﻿
    using Chain.BLL;
//    using ChainStock.Controls;
    using System;
    using System.Data;
    using System.Text;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    public partial class RptMemRecharge : PageBase
    {

  

        private void BindRechargeMoney()
        {
            MemRecharge bllMemRecharge = new MemRecharge();
            string sbWhere = this.QueryCondition() + "AND RechargeIsApprove=1 and MemRecharge.RechargeShopID = SysShop.ShopID and MemRecharge.RechargeMemID = Mem.MemID and Mem.MemLevelID=MemLevel.LevelID and MemRecharge.RechargeUserID = SysUser.UserID ";
            sbWhere = PubFunction.GetShopAuthority(base._UserShopID, "RechargeShopID", sbWhere);
            this.lblczMoney.Text = bllMemRecharge.GetRechargeMoney(sbWhere + string.Format(" and MemRecharge.RechargeCreateTime >= '{0}' and MemRecharge.RechargeCreateTime <= '{1}'", this.txtStartTime.Value, PubFunction.TimeEndDay(this.txtEndTime.Value))).ToString("f2");
            this.lbzsMoney.Text = bllMemRecharge.GetGiveMoney(sbWhere + string.Format(" and MemRecharge.RechargeCreateTime >= '{0}' and MemRecharge.RechargeCreateTime <= '{1}'", this.txtStartTime.Value, this.txtEndTime.Value)).ToString("f2");
        }

        protected void btnMemRechargeExcel_Click(object sender, EventArgs e)
        {
            int recordCounts;
            string orderBy;
            bool isAsc;
            StringBuilder strSqlSR;
            MemRecharge bllMemRecharge = new MemRecharge();
            DataTable db = null;
            string tag = this.Export.Value;
            string startTime = DateTime.Now.ToString("yyyy-MM") + "-01";
            string endTime = DateTime.Now.ToString();
            DateTime date = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01"));
            if (DateTime.TryParse(this.txtMRStart.Value, out date))
            {
                startTime = date.ToString();
            }
            date = DateTime.Now;
            if (DateTime.TryParse(this.txtMREnd.Value, out date))
            {
                endTime = date.ToString();
            }
            string name5 = tag;
            if (name5 != null)
            {
                if (!(name5 == "R"))
                {
                    if (!(name5 == "MR"))
                    {
                        if (!(name5 == "SR"))
                        {
                            return;
                        }
                        strSqlSR = new StringBuilder();
                        strSqlSR.Append(string.Format(" RechargeCreateTime>='{0}' AND RechargeCreateTime<='{1}' ", startTime, PubFunction.TimeEndDay(endTime)));
                        if (this.sltSRShop.Value != "")
                        {
                            strSqlSR.AppendFormat("and ShopID={0}", int.Parse(this.sltSRShop.Value));
                        }
                        if (this.sltSRType.Value != "")
                        {
                            strSqlSR.AppendFormat("and RechargeType={0} ", int.Parse(this.sltSRType.Value));
                        }
                        orderBy = "RechargeMemID";
                        isAsc = true;
                        switch (this.sltSROrderBy.Value)
                        {
                            case "":
                                orderBy = "RechargeShopID";
                                isAsc = false;
                                goto Label_07F0;

                            case "1":
                                orderBy = "TotalMoney";
                                isAsc = true;
                                goto Label_07F0;

                            case "2":
                                orderBy = "TotalMoney";
                                isAsc = false;
                                goto Label_07F0;

                            case "3":
                                orderBy = "RechargeTotalMoney";
                                isAsc = true;
                                goto Label_07F0;

                            case "4":
                                orderBy = "RechargeTotalMoney";
                                isAsc = false;
                                goto Label_07F0;

                            case "5":
                                orderBy = "RechargeTotalGive";
                                isAsc = true;
                                goto Label_07F0;

                            case "6":
                                orderBy = "RechargeTotalGive";
                                isAsc = false;
                                goto Label_07F0;

                            case "7":
                                orderBy = "RechargeCount";
                                isAsc = true;
                                goto Label_07F0;

                            case "8":
                                orderBy = "RechargeCount";
                                isAsc = false;
                                goto Label_07F0;
                        }
                        goto Label_07F0;
                    }
                }
                else
                {
                    StringBuilder strSqlR = new StringBuilder();
                    strSqlR.Append("1=1");
                    if (this.txtQueryMem.Value != "")
                    {
                        strSqlR.AppendFormat("and (MemCard = '{0}' or MemName like '%{0}%' or MemMobile = '{0}' or MemCardNumber = '{0}' )", this.txtQueryMem.Value);
                    }
                    if (this.sltMemLevelID.Value != "")
                    {
                        strSqlR.AppendFormat("and MemLevelID={0}", int.Parse(this.sltMemLevelID.Value));
                    }
                    if (this.sltShop.Value != "")
                    {
                        strSqlR.AppendFormat("and RechargeShopID={0}", int.Parse(this.sltShop.Value));
                    }
                    if (int.Parse(this.txtMoney.Value) != 0)
                    {
                        strSqlR.AppendFormat("and RechargeMoney" + this.sltMoney.Value + "{0}", this.txtMoney.Value);
                    }
                    if (this.txtStartTime.Value != "")
                    {
                        strSqlR.AppendFormat("and RechargeCreateTime>='{0}' ", this.txtStartTime.Value);
                    }
                    if (this.txtEndTime.Value != "")
                    {
                        strSqlR.AppendFormat("and RechargeCreateTime<='{0}'", PubFunction.TimeEndDay(this.txtEndTime.Value));
                    }
                    if (this.sltRecharge.Value != "")
                    {
                        strSqlR.AppendFormat("and RechargeType={0} ", int.Parse(this.sltRecharge.Value));
                    }
                    if (this.txtRemark.Value != "")
                    {
                        strSqlR.AppendFormat(" and RechargeRemark like '%{0}%' ", this.txtRemark.Value);
                    }
                    if (this.txtRechargeAccount.Value != "")
                    {
                        strSqlR.AppendFormat(" and RechargeAccount='{0}'", this.txtRechargeAccount.Value);
                    }
                    strSqlR.Append(" and MemRecharge.RechargeShopID = SysShop.ShopID and MemRecharge.RechargeMemID = Mem.MemID and Mem.MemLevelID=MemLevel.LevelID and MemRecharge.RechargeUserID = SysUser.UserID");
                    db = bllMemRecharge.GetListSP(0x2710, 1, out recordCounts, new string[] { PubFunction.GetShopAuthority(base._UserShopID, "RechargeShopID", strSqlR.ToString()) }).Tables[0];
                    DataExcelInfo.RechargeReportExcel(db, base._UserName);
                    return;
                }
                StringBuilder strSqlMR = new StringBuilder();
                strSqlMR.Append(string.Format("and RechargeCreateTime>='{0}' AND RechargeCreateTime<='{1}' ", startTime, PubFunction.TimeEndDay(endTime)));
                if (this.txtMRMem.Value != "")
                {
                    strSqlMR.AppendFormat("and (MemCard = '{0}' or MemName like '%{0}%' or MemMobile = '{0}' or MemCardNumber = '{0}' )", this.txtMRMem.Value);
                }
                if (this.sltMRShop.Value != "")
                {
                    strSqlMR.AppendFormat("and MemShopID={0}", int.Parse(this.sltMRShop.Value));
                }
                if (this.sltMRType.Value != "")
                {
                    strSqlMR.AppendFormat("and RechargeType={0} ", int.Parse(this.sltMRType.Value));
                }
                orderBy = "RechargeMemID";
                isAsc = true;
                switch (this.sltOrderBy.Value)
                {
                    case "":
                        orderBy = "RechargeMemID";
                        isAsc = false;
                        break;

                    case "1":
                        orderBy = "MemMoney";
                        isAsc = true;
                        break;

                    case "2":
                        orderBy = "MemMoney";
                        isAsc = false;
                        break;

                    case "3":
                        orderBy = "RechargeCount";
                        isAsc = true;
                        break;

                    case "4":
                        orderBy = "RechargeCount";
                        isAsc = false;
                        break;

                    case "5":
                        orderBy = "LastRechargeTime";
                        isAsc = true;
                        break;

                    case "6":
                        orderBy = "LastRechargeTime";
                        isAsc = false;
                        break;

                    case "7":
                        orderBy = "TotalMoney";
                        isAsc = true;
                        break;

                    case "8":
                        orderBy = "TotalMoney";
                        isAsc = false;
                        break;

                    case "9":
                        orderBy = "RechargeTotalMoney";
                        isAsc = true;
                        break;

                    case "10":
                        orderBy = "RechargeTotalMoney";
                        isAsc = false;
                        break;

                    case "11":
                        orderBy = "RechargeTotalGive";
                        isAsc = true;
                        break;

                    case "12":
                        orderBy = "RechargeTotalGive";
                        isAsc = false;
                        break;
                }
                db = bllMemRecharge.GetListSP1(0x2710, 1, orderBy, isAsc, out recordCounts, new string[] { PubFunction.GetShopAuthority(base._UserShopID, "MemShopID", strSqlMR.ToString()) }).Tables[0];
                DataExcelInfo.MemRechargeReportExcel(db, base._UserName);
            }
            return;
        Label_07F0:;
            db = bllMemRecharge.GetListSP2(0x2710, 1, orderBy, isAsc, out recordCounts, new string[] { PubFunction.GetShopAuthority(base._UserShopID, "ShopID", strSqlSR.ToString()) }).Tables[0];
            DataExcelInfo.ShopRechargeReportExcel(db, base._UserName);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                PubFunction.BindMemLevelSelect(this.sltMemLevelID, true);
                PubFunction.BindShopSelect(base._UserShopID, this.sltMRShop, true);
                PubFunction.BindShopSelect(base._UserShopID, this.sltSRShop, true);
                PubFunction.BindShopSelect(base._UserShopID, this.sltShop, true);
                PubFunction.BindShopSelect(base._UserShopID, this.sltShopChart, true);
                this.txtMoney.Value = "0";
                this.txtStartTime.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).ToString("yyyy-MM-dd");
                this.txtEndTime.Value = DateTime.Now.ToString("yyyy-MM-dd");
                this.txtMRStart.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).ToString("yyyy-MM-dd");
                this.txtMREnd.Value = DateTime.Now.ToString("yyyy-MM-dd");
                this.txtSRStart.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).ToString("yyyy-MM-dd");
                this.txtSREnd.Value = DateTime.Now.ToString("yyyy-MM-dd");
                this.txtMemStartTime.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).ToString("yyyy-MM-dd");
                this.txtMemEndTime.Value = DateTime.Now.ToString("yyyy-MM-dd");
                if ((PubFunction.curParameter.dataAuthority != 0) && (base._UserShopID > 1))
                {
                    this.sltShop.Value = this._UserShopID.ToString();
                    this.sltShop.Attributes.Add("disabled", "disabled");
                }
                this.BindRechargeMoney();
                PubFunction.AgainPrint(ref this.lblPrintTitle, ref this.lblPrintFoot, base._UserShopID);
                this.PointNum.Value = PubFunction.GetPointNum("JFBDBB");
                this.Revokable.Value = PubFunction.GetControlVisit(base._UserAuthority, "gvRptMemRecharge_hyRevoke", 0x15).ToString();
            }
        }

        protected string QueryCondition()
        {
            string strQueryMem = this.txtQueryMem.Value;
            string strMemLevelID = this.sltMemLevelID.Value;
            string strMemShopID = this.sltShop.Value;
            string strMoneySymbols = this.sltMoney.Value;
            string strMoney = (this.txtMoney.Value.Trim() != "") ? this.txtMoney.Value.Trim() : "0";
            decimal dclMoney = decimal.Parse(strMoney);
            string strRechargeType = this.sltRecharge.Value;
            string strRechargeAccount = PubFunction.RemoveSpace(this.txtRechargeAccount.Value);
            string strRemark = PubFunction.RemoveSpace(this.txtRemark.Value);
            StringBuilder strSql = new StringBuilder();
            strSql.Append("1=1");
            if (strQueryMem != "")
            {
                strSql.AppendFormat("and (MemCard = '{0}' or MemName like '%{0}%' or MemMobile = '{0}' or MemCardNumber = '{0}' )", strQueryMem);
            }
            if (strMemLevelID != "")
            {
                strSql.AppendFormat("and Mem.MemLevelID={0}", int.Parse(strMemLevelID));
            }
            if (strMemShopID != "")
            {
                strSql.AppendFormat("and RechargeShopID={0}", int.Parse(strMemShopID));
            }
            if (dclMoney != 0M)
            {
                strSql.AppendFormat("and RechargeMoney" + strMoneySymbols + "{0}", dclMoney);
            }
            if (this.txtStartTime.Value != "")
            {
                strSql.AppendFormat("and RechargeCreateTime>='{0}' ", this.txtStartTime.Value);
            }
            if (this.txtEndTime.Value != "")
            {
                string strEndTimes = PubFunction.TimeEndDay(this.txtEndTime.Value);
                strSql.AppendFormat("and RechargeCreateTime<='{0}'", strEndTimes);
            }
            if (strRechargeType != "")
            {
                strSql.AppendFormat("and RechargeType={0} ", int.Parse(strRechargeType));
            }
            if (strRemark != "")
            {
                strSql.AppendFormat(" and RechargeRemark like '%{0}%' ", strRemark);
            }
            if (strRechargeAccount != "")
            {
                strSql.AppendFormat(" and RechargeAccount='{0}'", strRechargeAccount);
            }
            return strSql.ToString();
        }
    }


